﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.EventSystems;

//虚拟摇杆 - 移动方向
public class VirtualStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler, IEndDragHandler
{
    //摇杆移动时为true
    public bool isVirtualStick=false;

    [Header("虚拟摇杆的底图")]
    [SerializeField] private RawImage touchBg;
    [Header("虚拟摇杆滑动方向的滑动器图")]
    [SerializeField] private RawImage touchTracker;

    private RectTransform touchTrackerRect;

    [Header("无效的中间区块占比:0.0f~1.0f")]
    public float deadZone = 0.2f;

    // 限制滑动的最大半径，以背景图的宽度的一半来定
    private float limitRadius;
    // 鼠标按下时，记录坐标位置
    private Vector2 pointerDown_RecordPos;
    private Canvas canvas;


    // 重置滑动器图的alpha值
    private void ResetTrackerAlpha(float a)
    {
        var c = touchTracker.color;
        c.a = a;
        touchTracker.color = c;
    }
    // 重置滑动器图的位置
    private void ResetTrackerPos()
    {
        isVirtualStick = false;
        //PlayerManager.moveDirection = Vector3.zero;//位置归零
        touchTracker.transform.localPosition = touchBg.transform.localPosition;
    }
    private void Awake()
    {
        var parentTRS = transform;
        while (parentTRS != null)
        {
            canvas = parentTRS.GetComponent<Canvas>();
            if (canvas != null) break;
            parentTRS = parentTRS.parent;
        }

        touchTrackerRect = touchTracker.GetComponent<RectTransform>();
    }
    // start 时，以背景图的宽度的一半作为限制滑动器移动的半径距离，并重置滑动器的alpha为：0.2f
    private void Start()
    {
        limitRadius = touchBg.rectTransform.rect.width * 0.5f;
        ResetTrackerAlpha(0f);
    }
    // 当有鼠标、触点按下时，记录背景图的世界坐标位置
    public void OnPointerDown(PointerEventData eventData)
    {
        pointerDown_RecordPos = eventData.position;
        ResetTrackerAlpha(1f);
    }
    // 触点单开时，重置滑动器的alpha
    public void OnPointerUp(PointerEventData eventData)
    {
        ResetTrackerAlpha(0f);
    }
    // 拖动时更新滑动器的位置
    public void OnDrag(PointerEventData eventData)
    {
        var deltaWorldPos = (eventData.position - pointerDown_RecordPos) / canvas.scaleFactor;
        var mag = deltaWorldPos.magnitude;
        // 超过deadZone才算作有效的位移摇杆
        if ((mag / limitRadius) > deadZone)
        {
            // 当总偏移向量模大于限制半径，则将向量模重置到半径限制的值
            if (mag > limitRadius) deltaWorldPos = deltaWorldPos.normalized * limitRadius;
            // 将总偏移的向量作为滑动器的局部坐标设置即可
            touchTrackerRect.anchoredPosition = deltaWorldPos;

            isVirtualStick = true;
            //Debug.Log("方向向量:"+ GetDirection());
            //Debug.Log("向量的模: " + GetLenPercent());
        }
        // 否则无效的位移将重置滑动器的位置
        else
        {
            ResetTrackerPos();
        }
    }
    // 拖动结束时重置滑动器图的位置
    public void OnEndDrag(PointerEventData eventData)
    {
        ResetTrackerPos();
    }
    // 获取输入的方向向量
    public Vector2 GetDirection()
    {
        return touchTracker.transform.localPosition.normalized;
    }
    // 获取输入的方向向量的模:0.0~1.0f
    public float GetLenPercent()
    {
        return (touchTracker.transform.localPosition.magnitude / limitRadius);
    }

    public void OpenPanel()
    {
        gameObject.SetActive(true);
    }
    public void ClosePanel()
    {
        gameObject.SetActive(false);
    }
}
